﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

/**
 * @file AnimationPack holds a record of an Animation
 *
 * @author LeonXie
 */

namespace Catsland.Core {

    /**
     *  @brief AnimationPack holds a record of an Animation
     *  */
    public class AnimationClip {
        // the name of the AnimationClip
        public String _name { get; set; }
        // the frame this animation begins
        public int _beginIndex { get; set; }
        // the frame this animation ends
        public int _endIndex { get; set; }
        /** 
         * clamp will stop playing at last frame,
         * loop will play this animation over and over again,
         * pingpoing will play from begin -> end and then end -> begin, and gose on
         * stop will stop playing and jump to the first frame
         */
        public enum PlayMode {
            CLAMP,
            LOOP,
            PINGPONG,
            STOP
        };
        public PlayMode _mode { get; set; }

        public AnimationClip(String name, int begin_index = 0, int end_index = 0, PlayMode mode = PlayMode.CLAMP) {
            _name = name;
            _beginIndex = begin_index;
            _endIndex = end_index;
            _mode = mode;
        }

        /**
         * @brief save the animationClip to an XML node
         *
         * @param node the parent node
         * @param doc the XML doc object
         * @return success?
         */
        public bool SaveToNode(XmlNode node, XmlDocument doc) {
            XmlElement clip = doc.CreateElement("AnimationClip");
            node.AppendChild(clip);

            clip.SetAttribute("name", _name);
            clip.SetAttribute("beginIndex", "" + _beginIndex);
            clip.SetAttribute("endIndex", "" + _endIndex);
            String mode = "";
            switch (_mode) {
                case PlayMode.CLAMP:
                    mode = "CLAMP";
                    break;
                case PlayMode.LOOP:
                    mode = "LOOP";
                    break;
                case PlayMode.PINGPONG:
                    mode = "PINGPONG";
                    break;
                case PlayMode.STOP:
                    mode = "STOP";
                    break;
            }
            clip.SetAttribute("mode", mode);

            return true;
        }

        /**
         * @brief create an AnimationClip from an XML node
         *
         * @param node the AnimationClip node
         * @param scene not need here
         * @return AnimationCliP
         */
        public static AnimationClip LoadFromNode(XmlNode node, Scene scene) {
            XmlElement clip = (XmlElement)node;
            String name = clip.GetAttribute("name");
            int beginIndex = int.Parse(clip.GetAttribute("beginIndex"));
            int endIndex = int.Parse(clip.GetAttribute("endIndex"));
            String modeString = clip.GetAttribute("mode");
            PlayMode mode = PlayMode.STOP;
            switch (modeString) {
                case "CLAMP":
                    mode = PlayMode.CLAMP;
                    break;
                case "LOOP":
                    mode = PlayMode.LOOP;
                    break;
                case "PINGPONG":
                    mode = PlayMode.PINGPONG;
                    break;
                case "STOP":
                    mode = PlayMode.STOP;
                    break;
            }
            AnimationClip newClip = new AnimationClip(name, beginIndex, endIndex, mode);
            return newClip;
        }
    }
}
